<template>
  <div class="f-r a-c f-w" style="margin-bottom: 18px">
    <el-upload action="" :accept="accept" class="file-upload" :before-upload="beforeUpload">
      <div class="f-r a-c" style="height: 100%">
        <div class="upload-icon-box f-r a-c j-c">
          <i class="el-icon-upload" />
        </div>
        <div class="upload-text-box f-c j-c">
          <div class="upload-text">点击上传</div>
<!--          <div class="upload-unit">docx、pdf、pptx</div>-->
        </div>
      </div>
    </el-upload>
    <div class="file-list f-r a-c f-w">
      <div v-for="(file,index) in fileList" :key="file.fileUrl" class="file-upload file-item">
        <div class="f-r a-c" style="height: 100%;position:relative;">
          <div class="upload-icon-box  f-r a-c j-c" style="background-color: #f3f6ff">
            <i class="el-icon-document" style="color: #2752f4" />
          </div>
          <div class="upload-text-box f-c j-c" :title="file.fileName">
            <div class="upload-text">{{ file.fileName }}</div>
            <div class="upload-unit" @click="handleClickFile(file.fileUrl)">点击查看</div>
          </div>
        </div>
        <div class="del-box f-r j-c a-c">
          <i class="el-icon-close" @click="delFile(index)" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { uploadFile } from '@/api'

export default {
  name: 'FileView',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: Array,
      default: () => []
    },
    accept: String
  },
  data() {
    return {
      file: '',
      fileList: []
    }
  },
  watch: {
    value: {
      handler(val) {
        this.fileList = val || []
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    // 上传之前: 获取file对象进行参数验证
    beforeUpload(file) {
      uploadFile(file).then(res => {
        this.fileList.push({
          fileName: file.name,
          fileType: file.type,
          fileSize: file.size,
          fileUrl: res.imgUrl
        })
        this.$emit('change', this.fileList)
      })
      return false
    },
    delFile(index) {
      this.fileList.splice(index, 1)
      this.$emit('change', this.fileList)
    },
    handleClickFile(url) {
      window.open(url, '_black')
    }
  }
}
</script>

<style lang="scss">
.file-upload {
    width: 330px;
    height: 80px;
    border-radius: 14px;
    border: 1px dashed #d9d9d9;
    background-color: #f3f6ff;
    padding: 10px;
    margin-right: 20px;
    margin-bottom: 20px;
    .el-upload {
        display: block;
    }
    .upload-icon-box {
        width: 60px;
        height: 60px;
        border-radius: 12px;
        background-color:#2752f4;
        color: #ffffff;
        font-size: 32px;
        margin-right: 20px;
        flex-shrink: 0;
    }
    .upload-text-box {
        height: 100%;
        text-align: left;
        cursor: pointer;
        flex-grow: 1;
        overflow: hidden;
        .upload-text {
            width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            color: #2752f4;
            font-size: 20px;
            margin-bottom: 5px;
        }
        .upload-unit {
            color: #666666;
            font-size: 14px;
        }
    }
}
.file-item {
    position: relative;
    background-color: #ffffff;
    border: 1px solid #2752f4;
    .upload-text-box {
        .upload-text {
            color: #333333;
        }
        .upload-unit {
            color: #2752f4;
        }
    }
    .del-box {
        background-color: #f55557;
        border-radius: 50%;
        position: absolute;
        font-size: 20px;
        width: 26px;
        height: 26px;
        top: -10px;
        right: -10px;
        cursor: pointer;
        z-index: 9;
        color: #ffffff;
    }
}

</style>
